import axios from 'axios';

const http = axios.create({
	baseURL: '/', // api 的 base_url
	timeout: 30000, // request timeout
	headers: {// 默认的
		'Content-Type': 'application/json'
	},
	responseType: 'json', // 默认的
})

http.interceptors.request.use(
	config => {
		return config
	},
	error => {
		// Do something with request error
		console.log(error) // for debug
		Promise.reject(error)
	}
)

// response interceptor
http.interceptors.response.use(
	response => {
		return response
	},
	error => {
		console.log(error)
		return Promise.reject(error)
	}
)

let configuration = {
	
}

export default {
	post: function(URI, data, headObject = {}, t = -1) {
		
		let header =  {"Accept": "application/json","Content-Type": "application/json"};
		headObject = {...header,...headObject};
		
		return new Promise((resolve,reject) => {
			
			let timeOutId = setTimeout(()=>{
				reject({'message':' time out'});
			},5000)
			
			http({ url: URI.ACTION, method: "post", data: data, headers:headObject}).then((response) => {
				clearTimeout(timeOutId);
				resolve(response);
				
			}).catch((response) => {
				clearTimeout(timeOutId);
				reject(response);
			});
		});
	},
	get: function() {
		
	}
}